/* __auth__ : KunLin */

package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.category.Category;
import com.cskaoyan.bean.category.CategoryExample;
import com.cskaoyan.bean.vo.goods.HomeIndexVO;
import com.cskaoyan.mapper.*;
import com.cskaoyan.utils.CopyModuleClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    AdMapper adMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    TopicMapper topicMapper;

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    SystemConfigMapper systemConfigMapper;


    @Override
    public HomeIndexVO index() {
        HomeIndexVO homeIndexVO = new HomeIndexVO();
        homeIndexVO.setData(new HomeIndexVO.DataDTO());
        homeIndexVO.getData().setNewGoodsList(new LinkedList<>());


        // 后台加载数量配置
        // 新品首发栏目商品显示数量
        int market_wx_index_new = 0;
        // 人气推荐栏目商品显示数量
        int market_wx_index_hot = 0;
        // 品牌制造商直供栏目品牌商显示数量
        int market_wx_index_brand = 0;
        //专题精选栏目显示数量
        int market_wx_index_topic = 0;

        //分类栏目显示数量(1级类目)
        int market_wx_catlog_list = 0;

        //分类栏目商品显示数量(1 级类目下而 2级类目显示的商品)
        int market_wx_catlog_goods = 0;

        List<SystemConfig> systemConfigs = systemConfigMapper.selectByExample(null);
        for (SystemConfig systemConfig : systemConfigs) {
            if ("market_wx_index_new".equals(systemConfig.getKeyName())) {
                market_wx_index_new = Integer.parseInt(systemConfig.getKeyValue());
            }
            if ("market_wx_index_hot".equals(systemConfig.getKeyName())) {
                market_wx_index_hot = Integer.parseInt(systemConfig.getKeyValue());
            }
            if ("market_wx_index_brand".equals(systemConfig.getKeyName())) {
                market_wx_index_brand = Integer.parseInt(systemConfig.getKeyValue());
            }
            if ("market_wx_index_topic".equals(systemConfig.getKeyName())) {
                market_wx_index_topic = Integer.parseInt(systemConfig.getKeyValue());
            }
            if ("market_wx_catlog_list".equals(systemConfig.getKeyName())) {
                market_wx_catlog_list = Integer.parseInt(systemConfig.getKeyValue());
            }
            if ("market_wx_catlog_goods".equals(systemConfig.getKeyName())) {
                market_wx_catlog_goods = Integer.parseInt(systemConfig.getKeyValue());
            }
        }


        // 新上线的商品
        GoodsExample newGoodsExample = new GoodsExample();
        newGoodsExample.createCriteria().andIsNewEqualTo(true).andDeletedEqualTo(false);
        List<Goods> newGoods = goodsMapper.selectByExample(newGoodsExample);
        int count = 0;
        for (Goods newGood : newGoods) {
            if (market_wx_index_new != 0 && count < market_wx_index_new) {
                HomeIndexVO.DataDTO.NewGoodsListDTO newGoodsListDTO = CopyModuleClassUtils.bean2vo(newGood, HomeIndexVO.DataDTO.NewGoodsListDTO.class);
                homeIndexVO.getData().getNewGoodsList().add(newGoodsListDTO);
                count++;
            }
        }


        homeIndexVO.getData().setCouponList(new LinkedList<>());
        //所有优惠券数据
        CouponExample couponExample = new CouponExample();
        couponExample.createCriteria().andDeletedEqualTo(false);
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        for (Coupon coupon : coupons) {
            HomeIndexVO.DataDTO.CouponDTO couponDTO = CopyModuleClassUtils.bean2vo(coupon, HomeIndexVO.DataDTO.CouponDTO.class);
            homeIndexVO.getData().getCouponList().add(couponDTO);
        }

        // groupList 这里直接设置一个空列表就好
        homeIndexVO.getData().setGrouponList(new LinkedList<>());


        homeIndexVO.getData().setChannel(new LinkedList<>());

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelEqualTo("L1").andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category : categories) {
            HomeIndexVO.DataDTO.ChannelDTO channelDTO = CopyModuleClassUtils.bean2vo(category, HomeIndexVO.DataDTO.ChannelDTO.class);
            homeIndexVO.getData().getChannel().add(channelDTO);
        }


        homeIndexVO.getData().setBanner(new LinkedList<>());
        // 所有的横幅 banner
        AdExample adExample = new AdExample();
        adExample.createCriteria().andDeletedEqualTo(false);
        List<Ad> ads = adMapper.selectByExample(adExample);
        for (Ad ad : ads) {
            HomeIndexVO.DataDTO.BannerDTO bannerDTO = CopyModuleClassUtils.bean2vo(ad, HomeIndexVO.DataDTO.BannerDTO.class);
            homeIndexVO.getData().getBanner().add(bannerDTO);
        }

        homeIndexVO.getData().setBrandList(new LinkedList<>());
        // 所有的品牌 brand
        BrandExample brandExample = new BrandExample();
        brandExample.createCriteria().andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);

        count = 0;
        for (Brand brand : brands) {
            if (market_wx_index_brand != 0 && market_wx_index_brand > count) {
                HomeIndexVO.DataDTO.BrandListDTO brandListDTO = CopyModuleClassUtils.bean2vo(brand, HomeIndexVO.DataDTO.BrandListDTO.class);
                homeIndexVO.getData().getBrandList().add(brandListDTO);
                count++;
            }
        }

        homeIndexVO.getData().setHotGoodsList(new LinkedList<>());
        // 所有的热卖商品
        GoodsExample hotGoodsExample = new GoodsExample();
        hotGoodsExample.createCriteria().andIsNewEqualTo(true).andDeletedEqualTo(false);
        List<Goods> hotGoods = goodsMapper.selectByExample(hotGoodsExample);
        count = 0;
        for (Goods hotGood : hotGoods) {
            if (market_wx_index_hot != 0 && count < market_wx_index_hot) {
                HomeIndexVO.DataDTO.HotGoodsListDTO hotGoodsListDTO = CopyModuleClassUtils.bean2vo(hotGood, HomeIndexVO.DataDTO.HotGoodsListDTO.class);
                homeIndexVO.getData().getHotGoodsList().add(hotGoodsListDTO);
                count++;
            }
        }

        homeIndexVO.getData().setTopicList(new LinkedList<>());
        // 所有Top数据
        TopicExample topicExample = new TopicExample();
        topicExample.createCriteria().andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        count = 0;
        for (Topic topic : topics) {
            if (market_wx_index_topic != 0 && market_wx_index_topic > count) {
                HomeIndexVO.DataDTO.TopicListDTO topicListDTO = CopyModuleClassUtils.bo2bean(topic, HomeIndexVO.DataDTO.TopicListDTO.class);
                homeIndexVO.getData().getTopicList().add(topicListDTO);
                count++;
            }
        }


        homeIndexVO.getData().setFloorGoodsList(new LinkedList<>());

        // 所有foot数据
        int l1 = 0;
        for (Category category : categories) {
            List<Category> categories1 = null;
            HomeIndexVO.DataDTO.FloorGoodsListDTO floorGoodsListDTO = null;
            if (market_wx_catlog_list != 0 && l1 < market_wx_catlog_list) {
                floorGoodsListDTO = CopyModuleClassUtils.bo2bean(category, HomeIndexVO.DataDTO.FloorGoodsListDTO.class);
                Integer id = floorGoodsListDTO.getId();
                CategoryExample categoryExample1 = new CategoryExample();
                categoryExample1.createCriteria().andPidEqualTo(id);
                categories1 = categoryMapper.selectByExample(categoryExample1);

                int g2 = 0;
                for (Category category1 : categories1) {
                    if (market_wx_catlog_goods != 0 && market_wx_catlog_goods > g2) {
                        GoodsExample goodsExample = new GoodsExample();
                        goodsExample.createCriteria().andCategoryIdEqualTo(category1.getId());
                        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
                        floorGoodsListDTO.setGoodsList(new LinkedList<>());
                        for (Goods good : goods) {
                            if (market_wx_catlog_goods > g2){
                                HomeIndexVO.DataDTO.FloorGoodsListDTO.GoodsListDTO goodsListDTO = CopyModuleClassUtils.bean2vo(good, HomeIndexVO.DataDTO.FloorGoodsListDTO.GoodsListDTO.class);
                                floorGoodsListDTO.getGoodsList().add(goodsListDTO);
                                g2++;
                            }
                        }
                    }
                }
                homeIndexVO.getData().getFloorGoodsList().add(floorGoodsListDTO);
            }
            l1++;
        }
        return homeIndexVO.success();
    }
}
